package org.sandbox2.domain.loan;

import java.util.Date;
import java.util.List;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooEntity
public class LoanDto {
	
	@Transient
	Long loanId;
	
	@Transient
    private String name;
	
	@Transient
    private String zipCode;
	
	@Transient
    private String address;

	@Transient
	private Double principal;

	@Transient
    private Integer paymentTime;

	@Transient
    private Double rate;

	@Transient
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "S-")
    private Date startDate;

	@Transient
    private String description;

    @Transient
    private List<PaymentDetail> paymentDetails;
    
    public static LoanDto findLoanDto(Long id) {
        if (id == null) return null;
//        return entityManager().find(LoanDto.class, id);

        // quick & dirty.        
        LoanDto dto = new LoanDto();
        Loan loan = Loan.findLoan(id);
        dto.setLoanId(id);
        dto.setPrincipal(loan.getPrincipal());
        dto.setRate(loan.getRate());
        dto.setStartDate(loan.getStartDate());
        dto.setPaymentTime(loan.getPaymentTime());
        dto.setPaymentDetails(loan.getPaymentDetails());
        dto.setDescription(loan.getDescription());
 
        Borrower borrower = Borrower.findBorrower(loan.getBorrowerId());
        dto.setZipCode(borrower.getZipCode());
        dto.setName(borrower.getName());
        dto.setAddress(borrower.getAddress());
        System.out.println(dto);
        return dto;
    }
}
